<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Pay as LibraryPay;
use app\common\model\second\Category;
use app\common\model\second\Order;
use app\common\model\second\Second;
use think\Db;

/**
 * 小区资讯接口
 */
class Message extends Api
{
    protected $noNeedLogin = ['category','lists','infos'];
    protected $noNeedRight = ['*'];

    /**
     * 分类列表
     * @ApiMethod (GET)
     * 
     */
    public function category()
    {
        if (!$this->request->isGet()) {
            $this->error('提交类型错误');
        }
        $list = Category::all();
        $this->success('', $list);
    }

    /**
     * @Author LK
     * @ApiTitle (列表)
     * @ApiMethod (get)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="分类ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $param = request()->param();
        extract($param);
        $data = Second::where(['category_id' => $id, 'status' => '0'])->paginate();
        $this->success('', $data);
    }
    /**
     * @Author LK
     * @ApiTitle (详情)
     * @ApiMethod (get)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function infos()
    {
        $param = request()->param();
        extract($param);
        $data = Second::get(['id' => $id]);
        $this->success('', $data);
    }

    /**
     * @Author LK
     * @ApiTitle (出价)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function add_order()
    {
        $param = request()->param();
        extract($param);
        $model = Second::get($id);
        $data = [
            'user_id' => $this->auth->id,
            'second_id' => $id,
            'price' => $model->price,
        ];
        Db::startTrans();
        try {
            $order = new Order;
            $order->allowField(true)->save($data);
            $model->status = 1;
            $model->save();
            queue('app/common/job/EsOrder', $order->id, 600, 'wmOrder');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('', $order->id);
    }
    /**
     * @Author LK
     * @ApiTitle (二手订单)
     * @ApiMethod (get)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturnParams (name="pay_status", type="integer", required=true, description="状态:all=全部,0=未支付,1=已支付")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function second_order()
    {
        $param = request()->param();
        extract($param);
        $order = new Order;
        if ($pay_status != 'all') {
            $order->where('pay_status', $pay_status);
        }
        $list = $order->with('second')->where('user_id', $this->auth->id)->paginate();
        $this->success('', $list);
    }
    /**
     * @Author LK
     * @ApiTitle (订单详情)
     * @ApiMethod (get)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function order_info()
    {
        $param = request()->param();
        extract($param);
        $data = Order::get($id, ['second']);
        $this->success('', $data);
    }
}
